package com.instwall.callnumzigbee;

import android.annotation.SuppressLint;
import android.os.Handler;
import android.os.Message;
import android.text.TextUtils;
import android.util.Log;

import com.instwall.callbase.tool.LogUtil;
import com.instwall.callnumlistener.CallNumNotification;
import com.instwall.callnumzigbee.broadcast.MyReceiver;

import org.jetbrains.annotations.NotNull;

import static com.instwall.callnumzigbee.ZigBeeConfig.STAG;

public class ZigBeeManager {

    public static ZigBeeManager instance;

    private ZigBeeManager(){
        MyReceiver.setListener(receiverListener);
        CallNumNotification.getCallNumNotification().addPlayerStateListener(playerStateListener);
    }

    public static  ZigBeeManager getInstance(){
        if (instance!=null) return instance;
        synchronized (ZigBeeManager.class){
            instance = new ZigBeeManager();
            Log.d(STAG, "getInstance is not null");
//            thread.start();
        }
        return instance;
    }

    static Thread thread = new Thread(new Runnable() {
        @Override
        public void run() {
            int index = 0;

                try {
                    Log.d(STAG, "thread run start  ");
                    String jsParams = constructZigBeeJsParams("1234");
                    if(!TextUtils.isEmpty(jsParams)){
                        CallNumNotification.getCallNumNotification().notificationClient(jsParams);
                        Log.d(STAG, "thread ");
                    }
                    index++;
                    Thread.sleep(4000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }


        }
    });

    private CallNumNotification.PlayerStateListener playerStateListener = new CallNumNotification.PlayerStateListener(){

        @Override
        public void onPlayerStateChanged() {
            Message message = Message.obtain();
            message.what = 1;
            mHandler.sendMessage(message);

        }
    };

    @SuppressLint("HandlerLeak")
    private Handler mHandler = new Handler() {

        //handleMessage为处理消息的方法
        public void handleMessage(@NotNull Message msg){
            super.handleMessage(msg);
            LogUtil.LogI(STAG , "mHandler   msg.what:" + msg.what );
            if (msg.what == 1) {

            }
        }
    };

    MyReceiver.ReceiverListener receiverListener = new MyReceiver.ReceiverListener() {
        @Override
        public void MessageReceiver(String msg) {
            LogUtil.LogI(STAG , "receiverListener   message:" + msg );
            String jsParams = constructZigBeeJsParams(msg);
            if(!TextUtils.isEmpty(jsParams)){
                CallNumNotification.getCallNumNotification().notificationClient(jsParams);
            }
        }
    };

    private static String constructZigBeeJsParams(String msg) {
        if(msg==null){
            return null;
        }

        return "window.CallService(\"start\")";
    }

}
